<template>
  <div>
    <div class="pageTopTitle">专家管理</div>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="专家姓名" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入专家姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="专家手机号" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入专家手机号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="app-container mt20">
      <el-row :gutter="10" class="mb20">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="expertList">
        <el-table-column label="专家姓名" align="center" prop="name"/>
        <el-table-column label="专家登陆账号" prop="account"/>
        <el-table-column label="专家手机号" align="center" prop="phone"/>
        <el-table-column label="创建时间" align="center" prop="createAt" width="180"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改专家对话框 -->
      <el-drawer
        :title="title"
        :visible.sync="open"
        size="35%"
        :wrapper-closable="false"
      >
        <div class="drawer-content">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="专家账号" prop="account">
              <el-input v-model="form.account" placeholder="请输入专家账号"/>
            </el-form-item>
            <el-form-item label="专家姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入专家姓名"/>
            </el-form-item>
            <el-form-item label="专家手机号" prop="phone">
              <el-input v-model="form.phone" maxlength="11" placeholder="请输入专家手机号"/>
            </el-form-item>
          </el-form>
        </div>
        <div class="drawer-footer">
          <el-button type="primary" style="width: 120px;" @click="submitForm" size="small">确 定</el-button>
          <el-button @click="cancel" style="width: 120px;" size="small">取 消</el-button>
        </div>
      </el-drawer>
    </div>
  </div>
</template>

<script>
import { listExpert, getExpert, delExpert, addExpert, updateExpert } from '@/api/web/expert'

export default {
  name: 'Expert',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专家表格数据
      expertList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        phone: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        account: [
          { required: true, message: '专家账号不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '专家姓名不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询专家列表 */
    getList() {
      this.loading = true
      listExpert(this.queryParams).then(response => {
        this.expertList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        account: null,
        name: null,
        phone: null,
        createAt: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加专家'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getExpert(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改专家'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExpert(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addExpert(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除专家编号为"' + ids + '"的数据项？').then(function() {
        return delExpert(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.drawer-content {
  padding: 0 30px 80px 20px;
}

.drawer-footer {
  position: absolute;
  bottom: 0;
  box-sizing: border-box;
  text-align: right;
  padding: 16px;
  width: 100%;
  border-top: 1px solid #eee;
  background-color: #fff;
}
 </style>
